JavaScript - Как извлечь имя файла из элемента управления вводом файла

Вывести свойство в ваш дочерний контроллер и наблюдать за ним из «родительского» контроллера. На ваш вопрос недостаточно информации, чтобы дать точный ответ, но он будет выглядеть примерно так:

public class ChildController {

    @FXML
    private TableView customerTable ;

    private final ReadOnlyObjectWrapper currentCustomer = new ReadOnlyObjectWrapper<>();

    public ReadOnlyObjectProperty currentCustomerProperty() {
        return currentCustomer.getReadOnlyProperty() ;
    }

    public Customer getCurrentCustomer() {
        return currentCustomer.get();
    }

    public void initialize() {
        // set up double click on table:

        customerTable.setRowFactory(tv -> {
            TableRow row = new TableRow<>();
            row.setOnMouseClicked(e -> {
                if (row.getClickCount() == 2 && ! row.isEmpty()) {
                    currentCustomer.set(row.getItem());
                }
            }
        });

    }
}

, а затем вы просто выполните:

Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(
        getClass().getResource("../layout/SearchCustomer.fxml"));
Parent parent = (Parent) fxmlLoader.load();

ChildController childController = fxmlLoader.getController();
childController.currentCustomerProperty().addListener((obs, oldCustomer, newCustomer) -> {
    // do whatever you need with newCustomer....
});

Scene scene = new Scene(parent);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(parent.getScene().getWindow());
stage.setScene(scene);
stage.resizableProperty().setValue(false);
stage.showAndWait();

альтернативный подход заключается в использовании Consumer в качестве обратного вызова в дочернем контроллере:

public class ChildController {

    @FXML
    private TableView customerTable ;

    private Consumer customerSelectCallback ;

    public void setCustomerSelectCallback(Consumer callback) {
        this.customerSelectCallback = callback ;
    }

    public void initialize() {
        // set up double click on table:

        customerTable.setRowFactory(tv -> {
            TableRow row = new TableRow<>();
            row.setOnMouseClicked(e -> {
                if (row.getClickCount() == 2 && ! row.isEmpty()) {
                    if (customerSelectCallback != null) {
                        customerSelectCallback.accept(row.getItem());
                    }
                }
            }
        });

    }
}

И в этой версии вы выполняете

Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(
        getClass().getResource("../layout/SearchCustomer.fxml"));
Parent parent = (Parent) fxmlLoader.load();

ChildController childController = fxmlLoader.getController();
childController.setCustomerSelectCallback(customer -> {
    // do whatever you need with customer....
});

Scene scene = new Scene(parent);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(parent.getScene().getWindow());
stage.setScene(scene);
stage.resizableProperty().setValue(false);
stage.showAndWait();

108
задан Marc Gravell 14 May 2009 в 12:55
поделиться

3 ответа

Assuming your has an id of upload this should hopefully do the trick:

var fullPath = document.getElementById('upload').value;
if (fullPath) {
    var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
    var filename = fullPath.substring(startIndex);
    if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
        filename = filename.substring(1);
    }
    alert(filename);
}
120
ответ дан 24 November 2019 в 03:25
поделиться
var path = document.getElementById('upload').value;//take path
var tokens= path.split('\\');//split path
var filename = tokens[tokens.length-1];//take file name
0
ответ дан 24 November 2019 в 03:25
поделиться
var pieces = str.split('\\');
var filename = pieces[pieces.length-1];
8
ответ дан 24 November 2019 в 03:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: